package ldh.im.base.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import ldh.common.PageResult;
import ldh.im.base.dao.ImGroupDao;
import ldh.im.base.pojo.ImGroup;
import ldh.im.base.pojo.ImUser;
import ldh.im.base.pojo.where.ImGroupWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2024-03-21 10:36:16
*/
@Service("imGroupService")
public class ImGroupService {

	@Resource
	private ImGroupDao imGroupDao;
	
	@Transactional
	public ImGroup insert(ImGroup imGroup) {
		int n = imGroupDao.insert(imGroup);
		if (n != 1) {
			throw new RuntimeException("ImGroup insert error!");
		}
		return imGroup;
	}
	
	@Transactional
	public ImGroup insertSelective(ImGroup imGroup) {
		int n = imGroupDao.insert(imGroup);
		if (n != 1) {
			throw new RuntimeException("ImGroup insertSelective error!");
		}
		return imGroup;
	}

	@Transactional
	public ImGroup updateById(ImGroup imGroup) {
		int  n = imGroupDao.updateById(imGroup);
		if (n != 1) {
			throw new RuntimeException("ImGroup update error!");
		}
        return imGroup;
     }

	@Transactional
	public ImGroup updateNotNullById(ImGroup imGroup) {
		int  n = imGroupDao.updateNotNullById(imGroup);
		if (n != 1) {
			throw new RuntimeException("ImGroup update error!");
		}
        return imGroup;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = imGroupDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("ImGroup delete error!");
		}
    }

	@Transactional(readOnly = true)
	public ImGroup getById(Long id) {
		ImGroup imGroup = imGroupDao.getById(id);
		return imGroup;
    }
    
	
	@Transactional(readOnly = true)
	public PageResult<ImGroup> findByImGroupWhere(ImGroupWhere imGroupWhere) {
		long total = imGroupDao.findTotalByImGroupWhere(imGroupWhere);
		List<ImGroup> imageList = new ArrayList<ImGroup>();
		if (total > 0) {
			imageList  = imGroupDao.findByImGroupWhere(imGroupWhere);
		}
		
		return new PageResult<ImGroup>(imGroupWhere, total, imageList);
	}


	@Transactional(readOnly = true)
	public List<ImGroup> getAllChangeImGroups(ImUser imUser, Date preRequestDate) {
		ImGroupWhere imGroupWhere = new ImGroupWhere();
		imGroupWhere.setOwnerId(imUser.getCode());
		imGroupWhere.setStartUpdateTime(preRequestDate);
		List<ImGroup> imGroupList = imGroupDao.getAllChangeGroups(imGroupWhere);
		return imGroupList;
	}
	
}
